const Service = require('egg').Service;

class UserService extends Service {
    async getList() {
        const {ctx,app}=this
        const {id,storeType,storeBy,keyword,pageIndex,pagesize}=ctx.query
        let sql = `select * from shop where 1=1 `
        let sqlCount = `select count(id) as total from shop where 1=1 `
        if(id){
            sql += `and id = ${id} `
            sqlCount += `and id = ${id} `
        }
        if(keyword){
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if(storeBy && storeType){
            sql += `and order by ${storeType} ${storeBy} `
        }
        if(pageIndex && pagesize){
            sql += `and limit ${(pageIndex-1)*pagesize},${pagesize} `
        }
        const data = await app.mysql.query(sql)
        const [{total}] = await app.mysql.query(sqlCount)
        return {
            data,
            total
        }
    }
    async addList(){
        const {ctx,app}=this
        const data = await app.mysql.insert("shop",{...ctx.request.body})
        return data
    }
   
    async editList(){
        const {ctx,app}=this
        const data = await app.mysql.update("shop",{...ctx.request.body})
        return data
    }
  
    async delete() {
        const { ctx,app } = this;
        let {id}=ctx.params;
        let res = await app.mysql.delete("shop",{id:id})
        if(res.affectedRows > 0){
            return "删除成功"
        }else{
            return "删除失败"
        }
    }
}

module.exports = UserService;